package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.core.entity.BaseDO;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IApplyToProgPositionDAO;
import org.jsola.hr.dto.ApplyToProgPositionAddDTO;
import org.jsola.hr.dto.ApplyToProgPositionUpdateDTO;
import org.jsola.hr.entity.ApplyToProgPositionDO;
import org.jsola.hr.query.ApplyToProgPositionQuery;
import org.jsola.hr.service.IApplyToProgPositionService;
import org.jsola.hr.vo.ApplyToProgPositionListVO;
import org.jsola.hr.vo.ApplyToProgPositionVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrApplyToProgPositionServiceImpl")
public class ApplyToProgPositionServiceImpl implements IApplyToProgPositionService {

    @Autowired
    private IApplyToProgPositionDAO applyToProgPositionDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public ApplyToProgPositionVO save(ApplyToProgPositionAddDTO applyToProgPositionAddDTO, TokenUser tokenUser) {
        // 转DO
        ApplyToProgPositionDO applyToProgPositionDO = applyToProgPositionAddDTO.to(ApplyToProgPositionDO.class);
        // 保存
        applyToProgPositionDO = save(applyToProgPositionDO, tokenUser.getSiteId(), tokenUser.getUserId());
        HrLogKit.logForSave("新增方案适用设置岗位", "saveApplyToProgPosition", applyToProgPositionDO.getId(), tokenUser);
        return applyToProgPositionDO.to(ApplyToProgPositionVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(ApplyToProgPositionUpdateDTO applyToProgPositionUpdateDTO, TokenUser tokenUser) {
        //转DO
        ApplyToProgPositionDO applyToProgPositionDO = applyToProgPositionUpdateDTO.to(ApplyToProgPositionDO.class);
        HrLogKit.logForUpdate("修改方案适用设置岗位", "updateApplyToProgPosition", applyToProgPositionDO.getId(), tokenUser);
        //根据主键更新，只更新非null值
        return updateByIdSelective(applyToProgPositionDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... applyToProgPositionIds) {
        HrLogKit.logForDelete("批量删除方案适用设置岗位", "deleteApplyToProgPositionByIds"
                , Arrays.toString(applyToProgPositionIds), tokenUser);
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) applyToProgPositionIds);
    }


    @Override
    public ApplyToProgPositionVO selectById(String applyToProgPositionId, String siteId) {
        ApplyToProgPositionDO applyToProgPositionDO = selectDOById(applyToProgPositionId, siteId);
        if (applyToProgPositionDO == null) {
            return null;
        }
        return applyToProgPositionDO.to(ApplyToProgPositionVO.class);
    }

    @Override
    public List<ApplyToProgPositionListVO> select(ApplyToProgPositionQuery applyToProgPositionQuery, String siteId) {
        List<ApplyToProgPositionDO> applyToProgPositionDOList = selectDO(applyToProgPositionQuery, siteId);
        if (CollectionUtils.isEmpty(applyToProgPositionDOList)) {
            return applyToProgPositionDOList == null ? null : new ArrayList<>();
        }
        return applyToProgPositionDOList.stream()
                .map(applyToProgPositionDO -> applyToProgPositionDO.to(ApplyToProgPositionListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(ApplyToProgPositionQuery applyToProgPositionQuery, String siteId) {
        Example example = buildExample(applyToProgPositionQuery, siteId);
        return applyToProgPositionDAO.selectCountByExample(example);
    }

    @Override
    public Page<ApplyToProgPositionListVO> selectPage(ApplyToProgPositionQuery applyToProgPositionQuery, String siteId) {
        Example example = buildExample(applyToProgPositionQuery, siteId);
        Page<ApplyToProgPositionDO> page = applyToProgPositionDAO.selectPageByExample(example,
                applyToProgPositionQuery.getPageNo(),
                applyToProgPositionQuery.getPageSize());

        return page.to(ApplyToProgPositionListVO.class);
    }

    @Override
    public ApplyToProgPositionDO selectDOById(String applyToProgPositionId, String siteId) {
        return listById(applyToProgPositionId, siteId);
    }

    @Override
    public List<ApplyToProgPositionDO> selectDO(ApplyToProgPositionQuery applyToProgPositionQuery, String siteId) {
        Example example = buildExample(applyToProgPositionQuery, siteId);
        return applyToProgPositionDAO.selectByExample(example);
    }


    @Override
    public Integer insertList(List<ApplyToProgPositionAddDTO> applyToProgPositionAddDTOList, String progId, TokenUser tokenUser) {
        if (CollectionUtils.isEmpty(applyToProgPositionAddDTOList)) {
            return 0;
        }
        List<ApplyToProgPositionDO> applyToProgPositionDOList = applyToProgPositionAddDTOList.stream().map(applyToProgPositionAddDTO -> {
            ApplyToProgPositionDO applyToProgPositionDO = applyToProgPositionAddDTO.to(ApplyToProgPositionDO.class);
            applyToProgPositionDO.setSiteId(tokenUser.getSiteId());
            applyToProgPositionDO.setCreateUserId(tokenUser.getUserId());
            applyToProgPositionDO.setGmtCreate(new Date());
            applyToProgPositionDO.setProgId(progId);
            return applyToProgPositionDO;
        }).collect(Collectors.toList());
        HrLogKit.logForSave("批量新增方案适用设置岗位", "insertListApplyToProgPosition", applyToProgPositionDOList
                .parallelStream().map(BaseDO::getId).collect(Collectors.joining(",", "[", "]")), tokenUser);
        return applyToProgPositionDAO.insertListAndSetId(applyToProgPositionDOList);
    }

    @Override
    public Integer deleteByProgId(String progId, TokenUser tokenUser) {
        ApplyToProgPositionDO applyToProgPositionDO = new ApplyToProgPositionDO();
        applyToProgPositionDO.setProgId(progId);
        applyToProgPositionDO.setSiteId(tokenUser.getSiteId());
        HrLogKit.logForDelete("根据方案id删除方案适用设置岗位（真实删除）", "deleteByProgId", progId, tokenUser);
        return applyToProgPositionDAO.delete(applyToProgPositionDO);
    }

    /**
     * 根据查询参数，构建example
     *
     * @param applyToProgPositionQuery 查询参数
     * @param siteId                   所属站点id
     * @return example
     */
    private Example buildExample(ApplyToProgPositionQuery applyToProgPositionQuery, String siteId) {
        Example example = new Example(ApplyToProgPositionDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        if (applyToProgPositionQuery.getProgId() != null) {
            example.and().andLike("progId", "%" + applyToProgPositionQuery.getProgId() + "%");
        }
        if (applyToProgPositionQuery.getPositionId() != null) {
            example.and().andLike("positionId", "%" + applyToProgPositionQuery.getPositionId() + "%");
        }
        if (applyToProgPositionQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", applyToProgPositionQuery.getCompanyId());
        }
        if (applyToProgPositionQuery.getGmtCreateStart() != null &&
                applyToProgPositionQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", applyToProgPositionQuery.getGmtCreateStart(),
                    applyToProgPositionQuery.getGmtCreateEnd());
        }
        if (applyToProgPositionQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + applyToProgPositionQuery.getCreateUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example, applyToProgPositionQuery.getOrders());
        return example;
    }
}




